<?php

namespace app\system\model;

use think\Model;
use think\facade\Request;
use think\facade\Config;

/**
 * 操作日志模型
 */
class LogOperate extends Model
{
    /**
     * 数据表名称
     * @var string
     */
    protected $name = 'system_log_operate';

    /**
     * 写入日志
     * @param string $name 操作名称
     * @param array $info 操作信息
     */
    public static function add($name, $info)
    {
        $open = Config::get('xp.log');
        if ($open) {
            $appName = app('http')->getName();
            $controller = strtolower(Request::controller());
            $action = Request::action();
            $ip = Request::ip();
            $command = $appName . '/' . $controller . '/' . $action;
            $obj = new self;
            $obj->save([
                'username' => USERNAME,
                'name' => $name,
                'command' => $command,
                'ip' => $ip,
                'dateline' => time(),
                'info' => json_encode($info, JSON_UNESCAPED_UNICODE),
            ]);
        }
    }
}
